package com.joy.abilityintnet.slice;

import com.joy.abilityintnet.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

public class MainAbilitySlice extends AbilitySlice {


    private Text backValueText;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        //start entering the NewAbilitySlice 在本页面刷新页面内容
        Component enterNewAbilitySliceButton = findComponentById(ResourceTable.Id_enter_newAbilitySlice);
        enterNewAbilitySliceButton.setClickedListener(listener -> presentForResult(new NewAbilitySlice() , new Intent(), 0));

        //start entering the SecondAbility page  进入新的页面
        Component enterSecondAbilityButton = findComponentById(ResourceTable.Id_enter_second);
        enterSecondAbilityButton.setClickedListener(component -> startEnterSecondAbility());

        //start entering the SecondAbility page  进入新的页面
        Component enterOneAbilityButton = findComponentById(ResourceTable.Id_enter_one);
        enterOneAbilityButton.setClickedListener(component -> startEnterOneAbility());

        backValueText = (Text) findComponentById(ResourceTable.Id_main_text);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * 返回到主页面时修改文字，文字是从其他地方通过参数窜过来的；传参形式 intent.setParam("key", "我从NewAbilitySlice跳回来咯");
     */
    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        // requestCode == 0 是presentForResult方法的 requestCode，
        if (requestCode != 0 || resultIntent == null) {
            return;
        }
        //key是参数名称
        String result = resultIntent.getStringParam("key");
        backValueText.setText(result);
    }

    /**
     * 进入到另一个页面
     */
    private void startEnterSecondAbility() {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder().withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName("com.joy.abilityintnet.SecondAbility")
                .build();
        intent.setOperation(operation);
        intent.setParam("key", "我从MainAbility进到了SecondAbility");
        startAbilityForResult(intent, 1);
    }

    /**
     * 进入到另一个页面
     */
    private void startEnterOneAbility() {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder().withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName("com.joy.abilityintnet.OneAbility")
                .build();
        intent.setOperation(operation);
        intent.setParam("key", "早上好，今天天气真不错。");
        startAbilityForResult(intent, 2);
    }
    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        if (resultCode != 0 || resultData == null) {
            return;
        }
        String result = resultData.getStringParam("key");
        backValueText.setText(result);
    }
}
